package com.product.android.ui.widget;
import java.util.Random;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nd.android.u.allcommon.R;
import com.product.android.business.Const.WBFLOW_TASK_OVERVIEW;
import com.product.android.commonInterface.allCommonInterfaceImpl.AllCommonCallOtherModel;


/**
 * ClassName:TaskOverView
 * Reason:	 生日祝福及送花任务完成时的下拉view
 * 
 * @author   林胜杰		
 * @version  
 * @since    Ver 1.1
 * @Date	 2013	2013-12-12		下午6:42:52
 *
 * @see 	 
 */

public class WbflowTaskOverView extends LinearLayout {
	private LinearLayout mlayoutBless;
	private TextView mtvBlessTitle;
	private TextView mtvBlessContent;
	
	private RelativeLayout mWbflowTaskOverView;
	private RelativeLayout mlayoutSendFlower;
	private TextView mtvSendFlowerMsg;
	private Button mbtnRetrieve;
	
	private Context mContext;
	
	public static final int MSG_BEGIN_TO_HIDE = 2013;	
	private final int DELAY_MILI_SECONDS = 4 * 1000;		//4秒之后收起
	
	public WbflowTaskOverView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initView();
	}
	
	//生日祝福任务完成
	private void setBlessComponent() {
		mlayoutSendFlower.setVisibility(View.GONE);
		mlayoutBless.setVisibility(View.VISIBLE);

		//随机取出一种祝福短语
		String strContent = "";
		int type = new Random().nextInt(3) + 1;
		switch (type) {
		case 1:
			strContent = mContext.getString(R.string.bless_message_one);
			break;
		case 2:
			strContent = mContext.getString(R.string.bless_message_two);
			break;
		case 3:
			strContent = mContext.getString(R.string.bless_message_third);
			break;
		}

		String strTitle = mContext.getString(R.string.bless_task_over);

		if (!TextUtils.isEmpty(strTitle) && !TextUtils.isEmpty(strContent)) {
			mtvBlessTitle.setText(strTitle);
			mtvBlessContent.setText(strContent);
		}
	}
	
	//鲜花任务完成
	private void setFlowerComponent(String strSendFlowerMsg){
		mlayoutSendFlower.setVisibility(View.VISIBLE);
		mlayoutBless.setVisibility(View.GONE);
		
		if(!TextUtils.isEmpty(strSendFlowerMsg)){
			mtvSendFlowerMsg.setText(strSendFlowerMsg);
		}
	}
	
	private void initView(){
		LayoutInflater.from(mContext).inflate(R.layout.wbflow_task_over_view, this, true);
		mlayoutBless = (LinearLayout)findViewById(R.id.layout_bless);
		mtvBlessTitle = (TextView)findViewById(R.id.tv_bless_title);
		mWbflowTaskOverView = (RelativeLayout) findViewById(R.id.layout_task_over);
		mWbflowTaskOverView.setOnClickListener(clickListener);
		
		mtvBlessTitle = (TextView)findViewById(R.id.tv_bless_title);
		mtvBlessContent = (TextView)findViewById(R.id.tv_bless_content);
		
		mlayoutSendFlower = (RelativeLayout)findViewById(R.id.layout_send_flower);
		mtvSendFlowerMsg = (TextView)findViewById(R.id.tv_send_flower_result);
		mbtnRetrieve = (Button)findViewById(R.id.btn_goto_retrieve);
		
	}
	
	private OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.layout_task_over) { 
				AllCommonCallOtherModel.MainModel.toReceivePoint(mContext);
			}
		}
	};
	
	//bShow:true 显示；false 隐藏
	public void beginAnimate(final boolean bShow) {
		TranslateAnimation animation = null;
		if (bShow) {
			setVisibility(View.VISIBLE);
			animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
					0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, -1.0f,
					Animation.RELATIVE_TO_SELF, 0.0f);
		} else {
			animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
					0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, -1.0f);
		}

		animation.setDuration(800L);

		animation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				if (!bShow) {
					setVisibility(View.GONE);
				}
				clearAnimation();
				
				if(bShow){
					handler.sendMessageDelayed(handler.obtainMessage(MSG_BEGIN_TO_HIDE), DELAY_MILI_SECONDS);
				}				
			}
		});

		startAnimation(animation);
	}
	
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg){
			super.handleMessage(msg);
			switch (msg.what){
			case MSG_BEGIN_TO_HIDE:
				beginAnimate(false);
				break;
			default:
				break;
			}
		}
	};
	
	
	@Override
	protected void onAttachedToWindow() {
		LocalBroadcastManager.getInstance(mContext).registerReceiver(
				mMessageReceiver, new IntentFilter(WBFLOW_TASK_OVERVIEW.ACTION_FLOWER_TASK_OVER_PROC));
		super.onAttachedToWindow();
	}

	@Override
	protected void onDetachedFromWindow() {
		LocalBroadcastManager.getInstance(mContext).unregisterReceiver(
				mMessageReceiver);
		super.onDetachedFromWindow();
	}

	//任务完成的广播接收
	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String strAction = intent.getStringExtra(WBFLOW_TASK_OVERVIEW.ACTIVITY_CONTEXT);
			if (!TextUtils.isEmpty(strAction)
					&& 0 == strAction.compareTo(mContext.toString())) {
				// 是同一个Context
				int iType = intent.getIntExtra(WBFLOW_TASK_OVERVIEW.TASK_OVER_TYPE, -1);
				if(WBFLOW_TASK_OVERVIEW.TYPE_BLESS_TASK_OVER == iType){
					setBlessComponent();
					beginAnimate(true);
				}else if(WBFLOW_TASK_OVERVIEW.TYPE_SEND_FLOWER_TASK_OVER == iType){
					setFlowerComponent(intent.getStringExtra(WBFLOW_TASK_OVERVIEW.STRING_FLOWER_TASK_OVER_MSG));
					beginAnimate(true);
				}
			}
		}
	};
}
